<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\user\api;

use app\common\controller\ApiController;
use app\common\model\Auth;
use app\user\model\User as UserModel;
use think\App;
use think\Db;
use think\facade\Hook;

/**
 * 用户公开控制器
 * @package app\user\admin
 */
class Index extends ApiController
{
    public function __construct(App $app = null)
    {
        parent::__construct($app);

    }
    /**
     * 根据设备编号获取对应的百度语音播报授权码
     */
    public function snToCode()
    {
        $device_sn = input('device_sn');
        $auth['device_sn'] = '';
        $auth['device_code'] = '';
        if($device_sn){
            $auth['device_sn'] = $device_sn;
            $auth['device_code'] = Db::name('mill_device_code')->where('device_sn', $device_sn)->value('code');
            //设备未绑定授权码，则自动绑定一个未使用的授权码
            if(empty($auth['device_code'])){
                $device_code = Db::name('mill_device_code')->where("device_sn = ''")->order('id asc')->find();
                if(!empty($device_code)){
                    $auth['device_code'] = $device_code['code'];
                    Db::name('mill_device_code')->where('id',$device_code['id'])->update(['device_sn'=>$device_sn]);
                }
            }
            //更新时间
            Db::name('mill_device_code')->where('code',$auth['device_code'])->update(['update_time'=>time()]);
        }
        success('',$auth);
    }
    /**
     * 用户登录
     * @return mixed
     */
    public function signin()
    {
        $auth = array('uid'=> '');
        if ($this->request->isPost()) {
            // 获取post数据
            $data = $this->request->post();

            // 验证数据
            if(empty($data['username']) || empty($data['password']))errors('请输入账号/密码',$auth);
            $result = $this->validate($data, 'User.signin');
            if(true !== $result){
                // 验证失败 输出错误信息
                errors($result,$auth);
            }

            //设备编号：用于获取百度语言播报授权码
            $device_sn = !empty($data['device_sn'])?$data['device_sn']:'';
            // 登录
            $UserModel = new UserModel;
            $user = $UserModel->loginApi($data['username'], $data['password'],$device_sn);
            $sns = empty(input('sns'))?'user':input('sns');
            $user['token'] = Auth::token_login("{$sns}_token",$user['uid']);

            if ($user) {
                success('登录成功',$user);
            } else {
                errors('登录失败，请重新登录',$auth);
            }
        }

        errors('登录异常',$auth);
    }

    /**
     * 退出登录
     */
    public function signout()
    {

        $user = $this->member;
        Auth::token_out_login('user_token',$user['uid']);

        success('已退出登录');
    }
}
